using UnityEngine;
public class PlayerCtrl : RoleInfo
{
    public float moveSpeed = 5f;  // 移动速度
    private CharacterController characterController; //角色控制器
    public Collider coll;
    private Vector3 velocity;
    public Animator anim; //动画控制器
    public bool isDead = false; //是否死亡
    public RoleSelect roleSelect;
    public int weaponID = 0;
    public float[] fireRate; // 射击频率
    public float FireDTime = 0f; // 下一次射击的时间
    private bool isFiring = false; // 是否正在发射子弹

    new void Start()
    {
        base.Start();
        characterController = GetComponent<CharacterController>();
        coll = GetComponent<Collider>();
        roleSelect.init();
        roleSelect.setWeapon(0);
    }
    void Update()
    {
        if (isDead) return;
        Move();

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            anim.SetTrigger("HG");
            weaponID = 0;
            roleSelect.setWeapon(0);
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            anim.SetTrigger("AR");
            weaponID = 1;
            roleSelect.setWeapon(1);
        }
        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            anim.SetTrigger("BZK");
            weaponID = 2;
            roleSelect.setWeapon(2);
        }
        if (Input.GetKeyDown(KeyCode.Alpha4))
        {
            anim.SetTrigger("GND");
            weaponID = 3;
            roleSelect.setWeapon(3);
        }

        // 检测鼠标左键是否按下或松开
        if (Input.GetMouseButtonDown(0)) isFiring = true;
        if (Input.GetMouseButtonUp(0)) isFiring = false;

        // 如果正在发射子弹并且时间到了，发射子弹
        if (FireDTime <= 0)
        {
            if (isFiring)
            {
                BroadcastMessage("Fire");
                FireDTime = fireRate[weaponID];
                anim.SetTrigger("fire");
            }
        }
        else FireDTime -= Time.deltaTime;
    }

    private void Move()
    {
        // 获取输入
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        // 获取摄像机当前的前方方向
        Vector3 cameraForward = Camera.main.transform.forward;
        cameraForward.y = 0; // 去掉 y 轴的分量
        cameraForward.Normalize(); // 归一化

        // 获取摄像机右侧方向
        Vector3 cameraRight = Camera.main.transform.right;

        // 计算移动方向
        Vector3 moveDirection = cameraForward * vertical + cameraRight * horizontal;
        moveDirection.Normalize(); // 确保方向向量的单位长度

        // 使玩家面向移动方向
        if (moveDirection != Vector3.zero)
        {
            anim.SetInteger("state", 1);
            transform.rotation = Quaternion.LookRotation(moveDirection, Vector3.up);
        }
        else
        {
            anim.SetInteger("state", 0);
        }
        // 应用移动
        characterController.Move(moveDirection * moveSpeed * Time.deltaTime);
        characterController.Move(velocity * Time.deltaTime);
    }

    public void die() //接收RoleInfo中发送的 die 事件
    {
        anim.SetTrigger("die"); //播放死亡动画
        isDead = true; // 标记为死亡
        coll.enabled = false; // 关闭碰撞
    }
}